package org.snail.common;

import org.apache.log4j.Logger;
import org.snail.constant.GroupConstant;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils2 {

    private static final Logger LOGGER =  Logger.getLogger(StringUtils2.class);

    public static boolean isContains(String str1, String str2) {
        boolean flag = false;
        if (str1 == null || str1.equals("") || str2 == null || str2.equals("")){
        }else {
            if (str1.contains(str2)){
                flag = true;
            }
        }
        return flag;
    }


        /**
         * 功能：判断是否有中文字符
         * @param str 字符串
         * @return boolean
         */
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * @function 将字符串转为int类型
     * @param str 需要转换数字的字符串
     * @return 转换成数字
     */
    public static int str2Integer(String str){
        int num = 0;
        if (str != null || "".equals(str))
            num = Integer.parseInt(str);
        return num;
    }

    /**
     * @function 将字符串转为Double类型
     * @param str
     * @return
     */
    public static Double str2Double(String str){
        Double num = 0.0;
        if (null != str || "".equals(str))
            num = Double.parseDouble(str);
        return num;
    }

    /**
     * @function 判断一个对象不为空
     * @param
     * @return
     */
    public static boolean objIsNull(Object str){
        return (str == null || "".equals(str));

    }

    /**
     * 判断字符串是否为空
     * @param str
     * @return boolean
     */
    public static boolean isNull(String str) {
        boolean flag = true;
        if(null != str && str.length() != 0) {
            flag = false;
        }
        return flag;
    }

    /**
     * 将List转成字符串
     * @param stringList 为List<String>
     * @return  字符串
     */
    public static String listToString(List<String> stringList) {
        if (stringList == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean flag = false;
        for (String string : stringList) {
            if (flag) {
                result.append(",");
            } else {
                flag = true;
            }
            result.append(string);
        }
        return result.toString();
    }

    //判断群组名字长度，钉钉API不允许大于20的字符串
    public static String judgeNameLength(String groupoNameStr) {
        if (null!=groupoNameStr && groupoNameStr.length()>GroupConstant.ADD_DING_NAME_MAX_LENGTH) {
            //1先去除空格
            groupoNameStr = groupoNameStr.replaceAll("\\s*", "");

            //2去除地名
            if(groupoNameStr.length()>GroupConstant.ADD_DING_NAME_MAX_LENGTH) {
                if(groupoNameStr.contains(GroupConstant.DING_BEIJIN_KEY)) {
                    groupoNameStr = groupoNameStr.replaceAll(GroupConstant.DING_BEIJIN_KEY, "");
                } else if(groupoNameStr.contains(GroupConstant.DING_HANGZHOU_KEY)) {
                    groupoNameStr = groupoNameStr.replaceAll(GroupConstant.DING_HANGZHOU_KEY, "");
                } else if(groupoNameStr.contains(GroupConstant.DING_SHANGHAI_KEY)) {
                    groupoNameStr = groupoNameStr.replaceAll(GroupConstant.DING_SHANGHAI_KEY, "");
                }
            }

            //3截取
            if(groupoNameStr.length()>GroupConstant.ADD_DING_NAME_MAX_LENGTH) {
                groupoNameStr = groupoNameStr.substring(0, 19) + "】";
            }

        }
        return groupoNameStr;
    }

}
